package leetcode.treeCode.week4.leetcode_501;

import java.util.ArrayList;

class Solution {
    ArrayList<Integer> list ;
    int maxCount = 0;
    int currentCount = 0;
    TreeNode prev = null;
    public int[] findMode(TreeNode root) {
        list = new ArrayList<>();
        dfs(root);
        int[] array = list.stream().mapToInt(i -> i).toArray();
        return array;

    }
    private void dfs(TreeNode node){
        if (node == null ) return ;
        dfs(node.left);
        // 中序遍历位置
        if (prev == null){
            currentCount = 1;
        }
        else  if (prev.val == node.val) {
            currentCount++;
        }
        else if (prev.val != node.val){
            currentCount = 1;
        }
        prev = node;
        if (currentCount > maxCount){
            maxCount = currentCount;
            list.clear();
            list.add(node.val);
        } else if (currentCount == maxCount) {
            list.add(node.val);
        }
        dfs(node.right);
    }
}